import 'dart:convert';
import 'dart:io';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:path_provider/path_provider.dart';
import '../models/anniversary.dart';
import '../models/memory.dart';
import '../models/photo_album.dart';
import '../models/diary_entry.dart';

class StorageService {
  static const String _relationshipStartDateKey = 'relationship_start_date';
  static const String _anniversariesKey = 'anniversaries';
  static const String _memoriesKey = 'memories';
  static const String _photoAlbumsKey = 'photo_albums';
  static const String _diaryEntriesKey = 'diary_entries';

  // 获取恋爱开始日期
  Future<DateTime?> getRelationshipStartDate() async {
    final prefs = await SharedPreferences.getInstance();
    final dateString = prefs.getString(_relationshipStartDateKey);

    if (dateString == null) {
      return null;
    }

    return DateTime.parse(dateString);
  }

  // 设置恋爱开始日期
  Future<void> setRelationshipStartDate(DateTime date) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_relationshipStartDateKey, date.toIso8601String());
  }

  // 获取纪念日列表
  Future<List<Anniversary>> getAnniversaries() async {
    final prefs = await SharedPreferences.getInstance();
    final jsonString = prefs.getString(_anniversariesKey);

    if (jsonString == null) {
      return [];
    }

    final List<dynamic> jsonList = jsonDecode(jsonString);
    return jsonList.map((json) => Anniversary.fromJson(json)).toList();
  }

  // 添加纪念日
  Future<void> addAnniversary(Anniversary anniversary) async {
    final anniversaries = await getAnniversaries();
    anniversaries.add(anniversary);

    final prefs = await SharedPreferences.getInstance();
    final jsonString = jsonEncode(anniversaries.map((a) => a.toJson()).toList());
    await prefs.setString(_anniversariesKey, jsonString);
  }

  // 更新纪念日
  Future<void> updateAnniversary(Anniversary anniversary) async {
    final anniversaries = await getAnniversaries();
    final index = anniversaries.indexWhere((a) => a.id == anniversary.id);

    if (index != -1) {
      anniversaries[index] = anniversary;

      final prefs = await SharedPreferences.getInstance();
      final jsonString = jsonEncode(anniversaries.map((a) => a.toJson()).toList());
      await prefs.setString(_anniversariesKey, jsonString);
    }
  }

  // 删除纪念日
  Future<void> deleteAnniversary(String id) async {
    final anniversaries = await getAnniversaries();
    anniversaries.removeWhere((a) => a.id == id);

    final prefs = await SharedPreferences.getInstance();
    final jsonString = jsonEncode(anniversaries.map((a) => a.toJson()).toList());
    await prefs.setString(_anniversariesKey, jsonString);
  }

  // 获取回忆列表
  Future<List<Memory>> getMemories() async {
    final prefs = await SharedPreferences.getInstance();
    final jsonString = prefs.getString(_memoriesKey);

    if (jsonString == null) {
      return [];
    }

    final List<dynamic> jsonList = jsonDecode(jsonString);
    return jsonList.map((json) => Memory.fromJson(json)).toList();
  }

  // 添加回忆
  Future<void> addMemory(Memory memory) async {
    final memories = await getMemories();
    memories.add(memory);

    final prefs = await SharedPreferences.getInstance();
    final jsonString = jsonEncode(memories.map((m) => m.toJson()).toList());
    await prefs.setString(_memoriesKey, jsonString);
  }

  // 更新回忆
  Future<void> updateMemory(Memory memory) async {
    final memories = await getMemories();
    final index = memories.indexWhere((m) => m.id == memory.id);

    if (index != -1) {
      memories[index] = memory;

      final prefs = await SharedPreferences.getInstance();
      final jsonString = jsonEncode(memories.map((m) => m.toJson()).toList());
      await prefs.setString(_memoriesKey, jsonString);
    }
  }

  // 删除回忆
  Future<void> deleteMemory(String id) async {
    final memories = await getMemories();
    memories.removeWhere((m) => m.id == id);

    final prefs = await SharedPreferences.getInstance();
    final jsonString = jsonEncode(memories.map((m) => m.toJson()).toList());
    await prefs.setString(_memoriesKey, jsonString);
  }

  // 获取相册列表
  Future<List<PhotoAlbum>> getPhotoAlbums() async {
    final prefs = await SharedPreferences.getInstance();
    final jsonString = prefs.getString(_photoAlbumsKey);

    if (jsonString == null) {
      return [];
    }

    final List<dynamic> jsonList = jsonDecode(jsonString);
    return jsonList.map((json) => PhotoAlbum.fromJson(json)).toList();
  }

  // 添加相册
  Future<void> addPhotoAlbum(PhotoAlbum album) async {
    final albums = await getPhotoAlbums();
    albums.add(album);

    final prefs = await SharedPreferences.getInstance();
    final jsonString = jsonEncode(albums.map((a) => a.toJson()).toList());
    await prefs.setString(_photoAlbumsKey, jsonString);
  }

  // 更新相册
  Future<void> updatePhotoAlbum(PhotoAlbum album) async {
    final albums = await getPhotoAlbums();
    final index = albums.indexWhere((a) => a.id == album.id);

    if (index != -1) {
      albums[index] = album;

      final prefs = await SharedPreferences.getInstance();
      final jsonString = jsonEncode(albums.map((a) => a.toJson()).toList());
      await prefs.setString(_photoAlbumsKey, jsonString);
    }
  }

  // 删除相册
  Future<void> deletePhotoAlbum(String id) async {
    final albums = await getPhotoAlbums();
    albums.removeWhere((a) => a.id == id);

    final prefs = await SharedPreferences.getInstance();
    final jsonString = jsonEncode(albums.map((a) => a.toJson()).toList());
    await prefs.setString(_photoAlbumsKey, jsonString);
  }

  // 获取日记列表
  Future<List<DiaryEntry>> getDiaryEntries() async {
    final prefs = await SharedPreferences.getInstance();
    final jsonString = prefs.getString(_diaryEntriesKey);

    if (jsonString == null) {
      return [];
    }

    final List<dynamic> jsonList = jsonDecode(jsonString);
    return jsonList.map((json) => DiaryEntry.fromJson(json)).toList();
  }

  // 添加日记
  Future<void> addDiaryEntry(DiaryEntry entry) async {
    final entries = await getDiaryEntries();
    entries.add(entry);

    final prefs = await SharedPreferences.getInstance();
    final jsonString = jsonEncode(entries.map((e) => e.toJson()).toList());
    await prefs.setString(_diaryEntriesKey, jsonString);
  }

  // 更新日记
  Future<void> updateDiaryEntry(DiaryEntry entry) async {
    final entries = await getDiaryEntries();
    final index = entries.indexWhere((e) => e.id == entry.id);

    if (index != -1) {
      entries[index] = entry;

      final prefs = await SharedPreferences.getInstance();
      final jsonString = jsonEncode(entries.map((e) => e.toJson()).toList());
      await prefs.setString(_diaryEntriesKey, jsonString);
    }
  }

  // 删除日记
  Future<void> deleteDiaryEntry(String id) async {
    final entries = await getDiaryEntries();
    entries.removeWhere((e) => e.id == id);

    final prefs = await SharedPreferences.getInstance();
    final jsonString = jsonEncode(entries.map((e) => e.toJson()).toList());
    await prefs.setString(_diaryEntriesKey, jsonString);
  }
}

